const nodemailer = require("nodemailer");

module.exports = function ({ host, service, auth, from }) {
  const config = {
    // 163邮箱 为smtp.163.com
    host: host || "smtp.qq.com", //默认qq邮箱
    service,
    //端口
    port: 465,
    auth,
  };
  const transporter = nodemailer.createTransport(config);
  let mailOptions = {
    from,
  };
  return async function (ctx, next) {
    ctx.mail = function sendMail(to, subject, text) {
      mailOptions = Object.assign(mailOptions, { to, subject, text, html });
      transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
          return console.log(error);
        }
        console.log("Message sent: %s", info.messageId);
      });
    };
    await next();
  };
};
